/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Asks;
import org.ipti.br.tag.entity.Asks_;
import org.ipti.br.tag.entity.Questions_;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ipti.br.tag.util.EncryptionUtil;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author ipti
 */
@Stateless
public class AsksFacade extends AbstractFacade<Asks> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AsksFacade() {
        super(Asks.class);
    }

    @Override
    public void create(Asks ask) {
        getEntityManager().persist(ask);
    }

    @Override
    public void edit(Asks asks) {
        asks.setSerialNumber(null);

        getEntityManager().merge(asks);
    }

    public void editForSynchronization(Asks asks) {
        getEntityManager().merge(asks);
    }

    public List<Asks> findAllOrder() {
        return findAllOrderBy(Asks_.name);
    }

    public List<Asks> asksPerQuestion(Long questionID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Asks> cq = cb.createQuery(Asks.class);
        Root<Asks> from = cq.from(Asks.class);
        Predicate predicate = cb.equal(from.get(Asks_.questions).get(Questions_.id), questionID);
        cq.where(cb.and(predicate));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
